/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com, guwei8576431@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE 
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 * 
 * Created on 2009-10-22
 *******************************************************************************/

package org.cgtw.scriptprofile.server.servlet.processor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import org.cgtw.scriptprofile.server.servlet.ScriptProfileRequest;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileResponseInfo;

public class ProcessorUtil {
	/**
	 * get remote inputstram
	 * 
	 * @param url
	 * @param request
	 * @return
	 */
	public static ScriptProfileResponseInfo getResponseInfo(URL url,
			ScriptProfileRequest request) {
		InputStream stream = null;
		String encoding = null;
		String contentType = null;
		Map<String, List<String>> responseHeader = null;
		String method = request.getMethod();
		if (method == null) {
			method = "GET";
		}
		if ("file".equalsIgnoreCase(url.getProtocol())) {
			String filePath = url.toString().substring(5);
			int paramOffset = filePath.indexOf('?');
			if (paramOffset > 0) {
				filePath = filePath.substring(0, paramOffset);
			}
			if (filePath.toLowerCase().endsWith(".js")) {
				contentType = "text/javascript";
			} else if (filePath.toLowerCase().endsWith(".htm")
					|| filePath.toLowerCase().endsWith(".html")) {
				contentType = "text/html";
			}
			try {
				stream = new FileInputStream(filePath);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else if ("http".equalsIgnoreCase(url.getProtocol())
				|| "https".equalsIgnoreCase(url.getProtocol())) {
			HttpURLConnection conn;
			try {
				conn = (HttpURLConnection) url.openConnection();
				conn.setDoOutput(true);
				conn.setDoInput(true);
				conn.setUseCaches(false);
				conn.setInstanceFollowRedirects(false);
				for (Map.Entry<String, String> entry : request
						.getRequestHeader().entrySet()) {
					conn.setRequestProperty(entry.getKey(), entry.getValue());
				}
				conn.setRequestMethod(method);
				if (method.equalsIgnoreCase("POST")) {
					conn.getOutputStream().write(
							request.getPostData().getBytes());
					conn.getOutputStream().flush();
				}
				String fileName = url.getFile();
				int offsetParam = fileName.indexOf('?');
				if (offsetParam > 0) {
					fileName = fileName.substring(0, offsetParam);
				}
				if (fileName.toLowerCase().endsWith(".js")) {
					contentType = "text/javascript";
				} else {
					contentType = conn.getContentType();
				}
				encoding = conn.getContentEncoding();
				stream = conn.getInputStream();
				responseHeader = conn.getHeaderFields();

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new ScriptProfileResponseInfo(encoding, stream, contentType,
				responseHeader);
	}

}
